//
//  USLivePhotoPreviewController.m
//  us
//
//  Created by karry on 2025/5/13.
//

#import "USPhotoPreviewController.h"
#import "USPhotoPreviewCell.h"
@interface USPhotoPreviewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, assign) BOOL didInitialLayout;



@end

@implementation USPhotoPreviewController


//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    [self setupView];


 
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.layout.itemSize = self.view.bounds.size;
      
      if (!self.didInitialLayout) {
          self.didInitialLayout = YES;
          [self.collectionView reloadData];
          
          dispatch_async(dispatch_get_main_queue(), ^{
              if (self.currentIndex < self.items.count) {
                  [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]
                                              atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                                      animated:NO];
              }
          });
      }
    
}

 
- (void)setupView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.itemSize = self.view.bounds.size;
    self.layout = layout;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor blackColor];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO; // 也可以考虑加上
    self.collectionView.alwaysBounceVertical = NO;
    
    [self.collectionView registerClass:[USPhotoPreviewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
    }];
}

- (void)deleteAction:(USPhotoModel *)model {
    MOAlertBaseView *alert = [[MOAlertBaseView alloc]initWithTitle:@"确定删除此照片吗" detailTitle:@"此操作不可撤销哦，请谨慎操作" contentView:nil leftButtonImageName:nil rightButtonImageName:nil autoDissmiss:YES tapDissmiss:YES];
    alert.titleLabel.textColor = UIColor.us_redColor;
    @weakify(self)
    alert.didClickIndexBlock = ^(NSInteger index) {
        @strongify(self)
        if (index == 1) {
            [self deletePhoto:model];
        }
    };
    
    [alert showInView:self.view];
}

- (void)deletePhoto:(USPhotoModel *)model{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"albumId"] = self.albumId;
    params[@"photoIdList"] = @[model.photoId];

    [[EMNetWork shareManager] em_post:API_AlbumDeletePhotos
                               params:params
                                  hud:nil
                            showError:YES
                              success:^(EMApiModel *api) {

        if (!api.success) {
            return;
        }

        if (self.deletePhotoBlock) {
            self.deletePhotoBlock(model);
        }
        [self dismissViewControllerAnimated:YES completion:nil];
     } fail:^{


    }];
}

#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.items.count;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    for (USPhotoPreviewCell *visibleCell in collectionView.visibleCells) {
        if (![visibleCell isEqual:cell]) {
            [visibleCell pauseIfPlaying];
        }
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    USPhotoPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.showBotTool = self.showBotTool;
    USPhotoModel *model = self.items[indexPath.item];
    [cell configureWithModel:model];
    
    @weakify(self)
    cell.onDeleteTapped = ^{
        @strongify(self)
        
        [self deleteAction:model];
    };

    cell.onCloseTapped = ^{
        @strongify(self)
        [self dismissViewControllerAnimated:YES completion:nil];
    };
    return cell;
}


@end
